package actions;

import javax.ejb.EJB;

import beans.BookManager;
import beans.BookManagerBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RequestItemsAction extends ActionSupport {

	private static final long serialVersionUID = -1188087737254332429L;
	
	private Integer[] isChecked;
	private Integer[] ownerId;
	
	@EJB
	private BookManager bookManager;
	
	public Integer[] getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(Integer[] ownerId) {
		this.ownerId = ownerId;
	}

	public Integer[] getIsChecked() {
		return isChecked;
	}
	
	public void setIsChecked(Integer[] isChecked) {
		this.isChecked = isChecked;
	}
	
	public String execute() {
		
		bookManager = new BookManagerBean();
		Integer userIdObj=(Integer)ActionContext.getContext().getSession().get("userId");
		if(userIdObj==null){
			this.addActionError("UserID is null");
			return ERROR;
		}
		
		String message = "";
		if ( isChecked != null )  {
			message = bookManager.addRequest(isChecked, userIdObj.intValue(),this.ownerId);
			if ( message.equalsIgnoreCase("success") ) {
				this.addActionMessage(isChecked.length + " requests made");
				return SUCCESS;
			}
		}
		this.addActionError(message);
		return "failedrequest";

	}

}
